#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "method_print.h"

void printHTTPResponseHeader()
{
    printf("Content-type: text/html;charset=utf-8\n\n");
}

void printHTMLStyles()
{
    printf( "<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/style_form.css\" />\n" );
}

void printHTMLHead(void)
{
    printf( "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n\"http://www.w3.org/TR/html4/loose.dtd\">\n" );
    printf( "<html>\n" );
    printf( "<head>\n" );
    printf( "<title>Form</title>\n" );
    printHTMLStyles();
    printf( "</head>\n" );
    printf( "<body>\n" );
}

void printHTMLEnd(void)
{
    printf( "</body>\n" );
    printf( "</html>\n" );
}

void printHTMLHeaderLine(void)
{
    printf("<form action=\"print_data.cgi\" method=\"POST\" name=\"form\" enctype=\"text/plain\">\n");
    printf("<div class=\"title\">\n");
    printf("<p class=\"header\">Sign Up</p>\n");
    printf("<p class=\"underhead\">It's free and always will be.</p>\n");
    printf("</div>\n");

    printf("<hr class=\"separator\" size=\"1\" />\n");
}

void printHTMLInputData(char* lastName, char* eMail, char* reEmail, char* password)
{
    printf("<div class=\"question\">\n");
    printf("<label for=\"lastName\">Last Name:</label>\n");
    printf("<input class=\"input\" type=\"text\" name=\"lastName\" value=\"%s\" id=\"lastName\" size=\"30\" readonly>\n", lastName);
    printf("</div>\n");

    printf("<div class=\"question\">\n");
    printf("<label for=\"email\">Your Email:</label>\n");
    printf("<input class=\"input\" type=\"text\" name=\"email\" value=\"%s\" id=\"email\" size=\"30\" readonly>\n", eMail);
    printf("</div>\n");

    printf("<div class=\"question\">\n");
    printf("<label for=\"reEmail\">Re-enter Email:</label>\n");
    printf("<input class=\"input\" type=\"text\" name=\"reEmail\" value=\"%s\" id=\"reEmail\" size=\"30\" readonly>\n", reEmail);
    printf("</div>\n");

    printf("<div class=\"question\">\n");
    printf("<label for=\"password\">New Password:</label>\n");
    printf("<input class=\"input\" type=\"password\" name=\"password\" value=\"%s\" id=\"password\" size=\"30\" maxlength=\"10\" readonly>\n", password);
    printf("</div>\n");
}

void printHTMLSexSelector(char* sex)
{
    printf("<div class=\"question sex\" id=\"sex\">\n");
    printf("<label for=\"select_sex\">I am:</label>\n");
    printf("<select name=\"sex\" id=\"select_sex\" readonly>\n");
	
	printf("<option>%s</option>\n", sex);

    printf("</select>\n");
    printf("</div>\n");
}


void printHTMLBirthDaySelector(char* month, char* day, char* year)
{
    printf("<div class=\"question birthday\" id=\"birthday\">\n");
    printf("<label for=\"month\">Birthday:</label>\n");
    printf("<select name=\"month\" id=\"month\" class=\"month\" readonly>\n");
    printf("<option selected>%s</option>\n", month);
    printf("</select>\n");

    printf("<select name=\"day\" id=\"day\" class=\"day\" readonly>\n");
    printf("<option selected>%s</option>\n", day);
    printf("</select>\n");

    printf("<select name=\"year\" id=\"year\" class=\"year\" readonly>\n");
    printf("<option selected>%s</option>\n", year);
    printf("</select>\n");
    printf("</div>\n");
}

void printHTMLFooter(void)
{
    printf("<div class=\"footer\">\n");
    printf("<a href=\"http://google.com\">\n");
    printf("<p>Why do I need to provide this?</p>\n");
    printf("</a>\n");
    printf("</div>\n");

    printf("<div class=\"button\">\n");
    printf("<input type=\"image\" src=\"../images/sign.png\" name=\"signUp\" disabled>\n");
    printf("</div>\n");

    printf("</form>\n");
}

int findIdentifier(int position, char* str)
{
    int i = position;
	while(i < strlen(str))
    {
		if(str[i] == '=')
			break;
        ++i; 
    }
	return i;
}

int getValue(int position, char* str, char* value)
{
    int i = findIdentifier(position, str);
	++i;
    int j = 0;
    if(str[i] != '\n')
	{
		while((str[i] != '\n') && (i <= (strlen(str)-1)))
		{
			value[j] = str[i];
			++j;
			++i;
		}
	}
	value[j] = '\0';
	return (i+1);
}

char myToLower(char ch)
{
	int res = (int)ch;
	if (ch < 128)
	{
		return tolower(ch);
	}
	else if (ch < 144)
	{
		res = (int)ch + 32;
	}
	else if (ch < 160)
	{
		res = (int)ch + 96;
	}
	
	return (char)res;
}

void toLowerStr(char* str)
{
	int i;
	for (i = 0; i < strlen(str); i++)
	{
		str[i] = myToLower(str[i]);
	}
}

void getDataFromFile(FILE * File, char* lastName, char* eMail, char* reEmail, char* password, char* sex, char* month, char* day, char* year)
{
	fgets(lastName, sizeof(lastName), File);
	fgets(eMail, sizeof(eMail), File);
	fgets(reEmail, sizeof(reEmail), File);
	fgets(password, sizeof(password), File);
	fscanf(File, "%s", sex);
	fscanf(File, "%s", month);
	fscanf(File, "%s", day);
	fscanf(File, "%s", year);
}